#! /bin/bash
set -e

./generate_config.sh

# Check env and set default value if needed
NACOS_SERVER_PORT=${NACOS_SERVER_PORT:-'8848'}

if [ -z "$NACOS_SERVER_HOST" ];then
    echo "Env NACOS_SERVER_HOST is not defined"
    exit 1
fi

if [ -z "$DUBHE_NACOS_NAMESPACE" ];then
    echo "Env DUBHE_NACOS_NAMESPACE is not defined"
    exit 1
fi

# Create namespace in nacos
curl -X POST "http://${NACOS_SERVER_HOST}:${NACOS_SERVER_PORT}/nacos/v1/console/namespaces" -d "customNamespaceId=${DUBHE_NACOS_NAMESPACE}&namespaceName=${DUBHE_NACOS_NAMESPACE}&namespaceDesc=\"namespace owned by Dubhe\""

for filename in `ls dubhe`
do
    echo $filename
    curl -X POST "http://${NACOS_SERVER_HOST}:${NACOS_SERVER_PORT}/nacos/v1/cs/configs" --data "tenant=${DUBHE_NACOS_NAMESPACE}&&dataId=${filename}&group=dubhe&type=yaml&content=`cat dubhe/${filename}`"
done

curl -X POST "http://${NACOS_SERVER_HOST}:${NACOS_SERVER_PORT}/nacos/v1/cs/configs" --data "tenant=${DUBHE_NACOS_NAMESPACE}&&dataId=kubeconfig.yaml&group=dubhe&type=yaml&content=`cat kubeconfig.yaml`"
